package com.example.cloudpracticeplatform.controller;

import com.example.cloudpracticeplatform.common.Response;
import com.example.cloudpracticeplatform.entity.Course;
import com.example.cloudpracticeplatform.service.CourseService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
@CrossOrigin
public class CourseController {

    @Resource
    private CourseService courseService;

    @PostMapping("/add")
    public Response<String> addCourse(@RequestBody Course course) {
        int result = courseService.addCourse(course);
        if (result > 0) {
            return Response.success("Course added successfully");
        } else {
            return Response.error("Failed to add course");
        }
    }

    @PostMapping("/update")
    public Response<String> updateCourse(@RequestBody Course course) {
        int result = courseService.updateCourse(course);
        if (result > 0) {
            return Response.success("Course updated successfully");
        } else {
            return Response.error("Failed to update course");
        }
    }

    @PostMapping("/delete")
    public Response<String> deleteCourse(@RequestParam Integer courseId) {
        int result = courseService.deleteCourse(courseId);
        if (result > 0) {
            return Response.success("Course deleted successfully");
        } else {
            return Response.error("Failed to delete course");
        }
    }

    @GetMapping("/{courseId}")
    public Response<Course> getCourseById(@PathVariable Integer courseId) {
        Course course = courseService.getCourseById(courseId);
        if (course != null) {
            return Response.success(course);
        } else {
            return Response.error("Course not found");
        }
    }

    @GetMapping("/list")
    public Response<List<Course>> getCourses(@RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "10") int pageSize) {
        List<Course> courses = courseService.getCourses(page, pageSize);
        return Response.success(courses);
    }
    
    
}